package com.dx.mes.service.special.service;

import com.dx.common.table.ppt.*;
import com.dx.mes.common.dao.IBisPathDao;
import com.dx.mes.common.dao.IRetSampTplDefDao;
import com.dx.mes.common.dao.IRetSlotDefDao;
import com.dx.mes.common.dao.IRetSmpDefDao;
import com.dx.mes.common.transcoder.util.AppContext;
import com.dx.mes.common.transcoder.util.JacksonUtil;
import com.dx.mes.common.transcoder.util.LogUtils;
import com.dx.mes.common.transcoder.util.Util;
import com.dx.mes.facade.special.service.IFspsmpdefService;
import com.dx.mes.facade.special.tx.Fspsmpdef.FspsmpdefI;
import com.dx.mes.facade.special.tx.Fspsmpdef.FspsmpdefIA;
import com.dx.mes.facade.special.tx.Fspsmpdef.FspsmpdefO;
import com.dx.mes.facade.special.tx.Fspsmpdef.Fspsmpdefexport;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import static com.dx.mes.common.bean.def.GenericDef.*;

/**
 * Created by tl on 2017/7/12.
 */
@Service("fspsmpdefService")
public class FspsmpdefService implements IFspsmpdefService {
    private LogUtils logUtils;
    private static String TX_NAME = "FSPSMPDEF";

    @Autowired
    private IRetSampTplDefDao fspsmprlnDao;
    @Autowired
    private IBisPathDao bisPathDao;
    @Autowired
    private IRetSlotDefDao retSlotDefDao;
    @Autowired
    private IRetSmpDefDao retSmpDefDao;


    public String subMainProc(String evt_no,String strInMsg) {
        AppContext.clear();
        AppContext.setCurrServiceName(FspsmpdefService.class.getSimpleName());
        AppContext.setCurrEventNumber(evt_no);
        logUtils = new LogUtils(FspsmpdefService.class);
        logUtils.info("[InTrx:" + strInMsg + "]");

        String outMsg = "";
        FspsmpdefI inTrx = new FspsmpdefI();
        FspsmpdefO outTrx = new FspsmpdefO();

        outTrx.setTrx_id(TX_NAME);
        outTrx.setType_id(TRX_OUT);
        outTrx.setRtn_code(String.valueOf(RETURN_CODE_SET_ERROR));

        try {
            inTrx = JacksonUtil.fromJson(strInMsg, FspsmpdefI.class);

            subMainProc2(inTrx.getAction_flg().toCharArray()[0],inTrx,outTrx);
            if (!outTrx.getRtn_code().equals(RETURN_CODE_OK)) {
                TransactionAspectSupport.currentTransactionStatus()
                        .setRollbackOnly();
            } else {
                retSmpDefDao.flushCurSession();
            }
        } catch (Exception ex) {
            outTrx.setRtn_mesg(ex.toString());
            outTrx.setRtn_code(RETURN_CODE_UNKNOWN);
            logUtils.error(Util.stackTraceToString(ex));
            TransactionAspectSupport.currentTransactionStatus()
                    .setRollbackOnly();
        } finally {
            outMsg = JacksonUtil.toJSONStr(outTrx);
            logUtils.info("[OutTrx:" + outTrx + "]");
        }
        return outMsg;
    }

    private void subMainProc2(char actionType,FspsmpdefI inTrx,FspsmpdefO outTrx) {

        switch (actionType) {
            case 'Q' :
                findSmpDefList(inTrx,outTrx);
                break;
            case 'Y' :
                active(inTrx,outTrx);
                break;
            case 'D' :
                deleteBySeqId(inTrx,outTrx);
                break;
            case 'A' :
                saveToDef(inTrx,outTrx);
                break;
            case 'T' :
                priority(inTrx,outTrx);
                break;

            default: {
                String gszMsgBuf = "Invalide type input: [" + actionType + "]";
                outTrx.setRtn_code(String.valueOf(E_FIPINQSHT_INVALID_INPUT));
                outTrx.setRtn_mesg(gszMsgBuf);
                return;
            }
        }
    }


    private void findSmpDefList(FspsmpdefI inTrx, FspsmpdefO outTrx) {
        FspsmpdefIA fspsmpsefIA = inTrx.getIary().get(0);
        String mdl_id_fk = fspsmpsefIA.getMdl_id_fk();
        String ope_no_fk = fspsmpsefIA.getOpe_no_fk();
        String ope_id_fk = fspsmpsefIA.getOpe_id_fk();
        String ope_ver_fk = fspsmpsefIA.getOpe_ver_fk();
        String tool_id_fk = fspsmpsefIA.getTool_id_fk();
        String path_id_fk = fspsmpsefIA.getPath_id_fk();
        String path_ver_fk = fspsmpsefIA.getPath_ver_fk();
        String samp_tpl_id_fk = fspsmpsefIA.getSamp_tpl_id_fk();

        String sql = "select * from ret_samp_def where 1 = 1";
        if (StringUtils.isNotBlank(mdl_id_fk)) {
            sql = sql + " and mdl_id_fk = '"+mdl_id_fk+"'";
        }
        if (StringUtils.isNotBlank(ope_no_fk)) {
            sql = sql + " and ope_no_fk = '"+ope_no_fk+"'";
        }
        if (StringUtils.isNotBlank(ope_id_fk)) {
            sql = sql + " and ope_id_fk = '"+ope_id_fk+"'";
        }
        if (StringUtils.isNotBlank(ope_ver_fk)) {
            sql = sql + " and ope_ver_fk = '"+ope_ver_fk+"'";
        }
        if (StringUtils.isNotBlank(tool_id_fk)) {
            sql = sql + "and tool_id_fk = '"+tool_id_fk+"'";
        }
        if (StringUtils.isNotBlank(path_id_fk)) {
            sql = sql + " and path_id_fk = '"+path_id_fk+"'";
        }
        if (StringUtils.isNotBlank(path_ver_fk)) {
            sql = sql + " and path_ver_fk = '"+path_ver_fk+"'";
        }

        if (StringUtils.isNotBlank(samp_tpl_id_fk)) {
            sql = sql + " and samp_tpl_id_fk = '"+samp_tpl_id_fk+"'";
        }
        List retSmpDefList = retSmpDefDao.findBySQL(sql, RetSampDef.class);
        if (retSmpDefList != null && retSmpDefList.size() > 0) {
            outTrx.setRtn_code(RETURN_CODE_OK);
            outTrx.setTbl_cnt(retSmpDefList.size());
            outTrx.setOary(retSmpDefList);
        } else {
            outTrx.setTbl_cnt(0);
            outTrx.setRtn_code(RETURN_CODE_OK);
        }

    }

    private void active(FspsmpdefI inTrx, FspsmpdefO outTrx) {


        FspsmpdefIA fspsmpdefIA = inTrx.getIary().get(0);
        String same_seq_id = fspsmpdefIA.getSamp_seq_id();
        RetSampDef retSampDef = retSmpDefDao.get(same_seq_id);
        if ("N".equalsIgnoreCase(retSampDef.getValid_flg())){
            retSampDef.setValid_flg("Y");
            retSmpDefDao.update(retSampDef);
            outTrx.setRtn_mesg("已经激活");
            outTrx.setRtn_code(RETURN_CODE_OK);
        } else {
            retSampDef.setValid_flg("N");
            retSmpDefDao.update(retSampDef);
            outTrx.setRtn_mesg("已经锁定");
            outTrx.setRtn_code(RETURN_CODE_OK);
        }


    }

    private void deleteBySeqId(FspsmpdefI inTrx, FspsmpdefO outTrx) {
        FspsmpdefIA fspsmpdefIA = inTrx.getIary().get(0);
        String[] samp_seq_ids = fspsmpdefIA.getSamp_seq_ids();
        for (String samp_seq_id : samp_seq_ids) {
            retSmpDefDao.deleteByKey(samp_seq_id);
        }
        outTrx.setRtn_code(RETURN_CODE_OK);
    }

    private void saveToDef(FspsmpdefI inTrx, FspsmpdefO outTrx) {
        FspsmpdefIA fspsmpsefIA = inTrx.getIary().get(0);
        String mdl_id_fk = fspsmpsefIA.getMdl_id_fk();             //p
        String ope_no_fk = fspsmpsefIA.getOpe_no_fk();             //p
        String ope_id_fk = fspsmpsefIA.getOpe_id_fk();               //p
        String ope_ver_fk = fspsmpsefIA.getOpe_ver_fk();             //p
        String tool_id_fk = fspsmpsefIA.getTool_id_fk();             //p
        String path_id_fk = fspsmpsefIA.getPath_id_fk();             //p
        String path_ver_fk = fspsmpsefIA.getPath_ver_fk();             //p
        String samp_tpl_id_fk = fspsmpsefIA.getSamp_tpl_id_fk();             //p
        String rsv_batch_qty = fspsmpsefIA.getRsv_batch_qty();
        String evt_note = fspsmpsefIA.getEvt_note();
        String evt_usr = fspsmpsefIA.getEvt_usr();
        String samp_qty = fspsmpsefIA.getSamp_qty();

        String sql = "select * from ret_samp_def where 1 = 1";
        if (StringUtils.isNotBlank(mdl_id_fk)) {
            sql = sql + " and mdl_id_fk = '"+mdl_id_fk+"'";
        }
        if (StringUtils.isNotBlank(ope_no_fk)) {
            sql = sql + " and ope_no_fk = '"+ope_no_fk+"'";
        }
        if (StringUtils.isNotBlank(ope_id_fk)) {
            sql = sql + " and ope_id_fk = '"+ope_id_fk+"'";
        }
        if (StringUtils.isNotBlank(ope_ver_fk)) {
            sql = sql + " and ope_ver_fk = '"+ope_ver_fk+"'";
        }
        if (StringUtils.isNotBlank(tool_id_fk)) {
            sql = sql + "and tool_id_fk = '"+tool_id_fk+"'";
        }
        if (StringUtils.isNotBlank(path_id_fk)) {
            sql = sql + " and path_id_fk = '"+path_id_fk+"'";
        }
        if (StringUtils.isNotBlank(path_ver_fk)) {
            sql = sql + " and path_ver_fk = '"+path_ver_fk+"'";
        }

        if (StringUtils.isNotBlank(samp_tpl_id_fk)) {
            sql = sql + " and samp_tpl_id_fk = '"+samp_tpl_id_fk+"'";
        }
        List<RetSampDef> retSmpDefList = retSmpDefDao.findBySQL(sql, RetSampDef.class);
        if (retSmpDefList.size()>0) {
            if (StringUtils.isNotBlank(tool_id_fk)) {
                for (RetSampDef retSampDef : retSmpDefList) {
                    if (tool_id_fk.equals(retSampDef)) {
                        outTrx.setRtn_code(RETURN_CODE_SET_ERROR);
                        outTrx.setRtn_mesg("业务主键重复");
                        return;
                    }
                }
            }
            if (StringUtils.isNotBlank(mdl_id_fk)) {
                outTrx.setRtn_code(RETURN_CODE_SET_ERROR);
                outTrx.setRtn_mesg("业务主键重复");
                return;
            }
        }
        RetSampDef retSampDef = new RetSampDef();
        String samp_seq_id = UUID.randomUUID().toString().replace("-", "");
        String unq_seq_id = UUID.randomUUID().toString().replace("-", "").substring(0, 25);
        retSampDef.setSamp_seq_id(samp_seq_id);
        retSampDef.setUnq_seq_id(unq_seq_id);
        retSampDef.setMdl_id_fk(mdl_id_fk);
        retSampDef.setOpe_no_fk(ope_no_fk);
        retSampDef.setOpe_ver_fk(ope_ver_fk);
        retSampDef.setSamp_tpl_id_fk(samp_tpl_id_fk);
        retSampDef.setRsv_batch_qty(Integer.parseInt(rsv_batch_qty));
        retSampDef.setEvt_cate("EVCT");
        retSampDef.setCrt_note(evt_note);
        retSampDef.setEvt_usr(evt_usr);
        retSampDef.setEvt_timestamp(new Timestamp(new Date().getTime()));
        retSampDef.setCrt_timestamp(new Timestamp(new Date().getTime()));
        //4.设备为空SAMP_TYP赋值2，否则赋值1；
        if (StringUtils.isNotBlank(tool_id_fk)) {
            retSampDef.setSamp_typ(1);
        } else {
            retSampDef.setSamp_typ(2);
        }
        //5.默认VALID_FLG=N；
        retSampDef.setValid_flg("N");
        //SAMP_QTY和SAMP_TIME只能设定1个，设定SAMP_QTY时SAMP_CALC_TYP=1，设定SAMP_TIME时SAMP_CALC_TYP=2；
        if (StringUtils.isNotBlank(samp_qty)) {
            retSampDef.setSamp_qty(Integer.parseInt(samp_qty));
            retSampDef.setSamp_calc_typ(1);
        }
        /*7.当有选择工艺路线，工艺版本时候，需要跟表RET_SAMP_TPL_DEF中的工艺路线和工艺版本进行比对，筛选表RET_SAMP_TPL_DEF
         中工艺路线和工艺版本相同的或者工艺路线和工艺版本为空的记录；*/
        if (StringUtils.isNotBlank(path_id_fk) && StringUtils.isNotBlank(path_ver_fk)) {
            int ctn = 1;
            String prl = "select * from ret_samp_tpl_def where 1 = 1 and path_id_fk = '"+path_id_fk+"' and path_ver_fk = '"+path_ver_fk+"'";
            List<RetSampTplDef> samoDefList = fspsmprlnDao.findBySQL(prl, RetSampTplDef.class);
            for (RetSampTplDef retSampTplDef : samoDefList) {
                if (path_id_fk.equals(retSampTplDef.getPath_id_fk()) && path_ver_fk.equals(retSampTplDef.getPath_ver_fk())) {
                    ctn = ctn + 1;
                }
            }
            String prl1 = "select * from ret_samp_tpl_def";
            List<RetSampTplDef> samoDefList1 = fspsmprlnDao.findBySQL(prl1, RetSampTplDef.class);
            for (RetSampTplDef retSampTplDef1 : samoDefList1) {
                if (StringUtils.isBlank(retSampTplDef1.getPath_id_fk()) && StringUtils.isBlank(retSampTplDef1.getPath_ver_fk())) {
                    ctn = ctn + 1;
                }
            }
            if (ctn == 1) {
                outTrx.setRtn_code(RETURN_CODE_SET_ERROR);
                outTrx.setRtn_mesg("工艺路线或版本与设定不符");
                return;
            } else {
                retSampDef.setPath_id_fk(path_id_fk);
                retSampDef.setPath_ver_fk(path_ver_fk);
            }
        }
        //8.比对RET_SAMP_DEF表的OPE_ID_FK与RET_SAMP_TPL_DEF表的OPE_PRTY=1的记录的OPE_ID_FK是否一致，
        // 不一致则报错：此模板开始站点与当前设定不符，无法保存；
        if (StringUtils.isNotBlank(ope_id_fk)) {
            int qty = 1;
            String ql = "select * from ret_samp_tpl_def where 1 = 1 and ope_prty = 1";
            List<RetSampTplDef> retSampTplDefs = fspsmprlnDao.findBySQL(ql, RetSampTplDef.class);
            if (retSampTplDefs == null || retSampTplDefs.size() < 1) {
                outTrx.setRtn_code(RETURN_CODE_SET_ERROR);
                outTrx.setRtn_mesg("此模板开始站点与当前设定不符，无法保存");
                return;
            }
            for (RetSampTplDef retSampTplDef : retSampTplDefs) {
                if (ope_id_fk.equals(retSampTplDef.getOpe_id_fk())) {
                    qty = qty + 1;
                }
            }
            if (qty == 1) {
                outTrx.setRtn_code(RETURN_CODE_SET_ERROR);
                outTrx.setRtn_mesg("此模板开始站点与当前设定不符，无法保存");
                return;
            } else {
                retSampDef.setOpe_id_fk(ope_id_fk);
            }
        }
        //SAMP_PRTY依照数据录入顺序进行优先级默认；
        String hq = "select * from ret_samp_def order by samp_prty desc";
        List<RetSampDef> list = retSmpDefDao.findBySQL(hq, RetSampDef.class);
        if (list != null && list.size() > 0) {
            RetSampDef retSampDef1 = list.get(0);
            Integer samp_prty = retSampDef1.getSamp_prty();
            retSampDef.setSamp_prty(samp_prty+1);
        } else {
            retSampDef.setSamp_prty(1);
        }
        retSmpDefDao.save(retSampDef);
        outTrx.setRtn_code(RETURN_CODE_OK);

    }

    private void priority(FspsmpdefI inTrx, FspsmpdefO outTrx) {
        FspsmpdefIA fspsmpdefIA = inTrx.getIary().get(0);
        String same_seq_id = fspsmpdefIA.getSamp_seq_id();
        RetSampDef retSampDef = retSmpDefDao.get(same_seq_id);
        Integer samp_typ = retSampDef.getSamp_typ();
        String mdl_id_fk = retSampDef.getMdl_id_fk();
        String path_id_fk = retSampDef.getPath_id_fk();
        String path_ver_fk = retSampDef.getPath_ver_fk();
        String ope_id_fk = retSampDef.getOpe_id_fk();
        String ope_no_fk = retSampDef.getOpe_no_fk();
        String ope_ver_fk = retSampDef.getOpe_ver_fk();
        String tool_id_fk = retSampDef.getTool_id_fk();

        /*3.如果记录的SAMP_TYP=1，则根据MDL_ID_FK，PATH_ID_FK，PATH_VER_FK，OPE_NO_FK，OPE_ID_FK，OPE_VER_FK，TOOL_ID_FK
        条件查询出所有记录，进行优先级变更操作;*/
        if (samp_typ == 1) {
            String sql = "select * from ret_samp_def where 1 = 1";
            if (StringUtils.isNotBlank(mdl_id_fk)) {
                sql = sql + " and mdl_id_fk = '"+mdl_id_fk+"'";
            }
            if (StringUtils.isNotBlank(ope_no_fk)) {
                sql = sql + " and ope_no_fk = '"+ope_no_fk+"'";
            }
            if (StringUtils.isNotBlank(ope_id_fk)) {
                sql = sql + " and ope_id_fk = '"+ope_id_fk+"'";
            }
            if (StringUtils.isNotBlank(ope_ver_fk)) {
                sql = sql + " and ope_ver_fk = '"+ope_ver_fk+"'";
            }
            if (StringUtils.isNotBlank(tool_id_fk)) {
                sql = sql + "and tool_id_fk = '"+tool_id_fk+"'";
            }
            if (StringUtils.isNotBlank(path_id_fk)) {
                sql = sql + " and path_id_fk = '"+path_id_fk+"'";
            }
            if (StringUtils.isNotBlank(path_ver_fk)) {
                sql = sql + " and path_ver_fk = '"+path_ver_fk+"'";
            }
            List<RetSampDef> retSampDefList = retSmpDefDao.findBySQL(sql, RetSampDef.class);
            if (retSampDefList != null && retSampDefList.size() > 0) {
                for (RetSampDef sampDef : retSampDefList) {
                    sampDef.setSamp_prty(sampDef.getSamp_prty()+1);
                }
            } else {
                outTrx.setRtn_code(RETURN_NULL_ERROR);
                outTrx.setRtn_mesg("没有找到需要提升的数据！");
                return;
            }
            outTrx.setRtn_code(RETURN_CODE_OK);
        } else if (samp_typ == 2) {
            String sql = "select * from ret_samp_def where 1 = 1";
                sql = sql + " and mdl_id_fk is null";
                sql = sql + " and ope_no_fk is null";
                sql = sql + " and ope_id_fk is null";
                sql = sql + " and ope_ver_fk is null";
                sql = sql + " and tool_id_fk is null";
                sql = sql + " and path_id_fk is null";
                sql = sql + " and path_ver_fk is null";
            List<RetSampDef> retSampDefList = retSmpDefDao.findBySQL(sql, RetSampDef.class);
            if (retSampDefList != null && retSampDefList.size() > 0) {
                for (RetSampDef sampDef : retSampDefList) {
                    sampDef.setSamp_prty(sampDef.getSamp_prty()+1);
                }
            } else {
                outTrx.setRtn_code(RETURN_NULL_ERROR);
                outTrx.setRtn_mesg("没有找到需要提升的数据！");
                return;
            }
            outTrx.setRtn_code(RETURN_CODE_OK);
        }

    }

    public List<RetSampDef> export(String samp_seq_id) {
        FspsmpdefI fspsmpdefI = JacksonUtil.fromJson(samp_seq_id, FspsmpdefI.class);
        String samp_id = fspsmpdefI.getSamp_seq_id();
        String[] samp_ids = samp_id.split(",");
        ArrayList<RetSampDef> retSampDefs = new ArrayList<>();
        for (int i = 0; i < samp_ids.length; i++) {
            String sampId = samp_ids[i];
            RetSampDef retSampDef = retSmpDefDao.get(sampId);
            retSampDefs.add(retSampDef);
        }
        return retSampDefs;
    }

    @Override
    public Fspsmpdefexport insertBeanWithVerify(List<RetSampDef> retSampDefList) {
        Fspsmpdefexport fspsmpdefexport = new Fspsmpdefexport();
        ArrayList<Integer> primerIndex = new ArrayList<>();
        for (int i = 0; i < retSampDefList.size(); i++) {
            String samp_seq_id = UUID.randomUUID().toString().replace("-", "");
            String unq_seq_id = UUID.randomUUID().toString().replace("-", "").substring(0, 25);
            RetSampDef retSampDef = retSampDefList.get(i);
            String valid_flg = retSampDef.getValid_flg();
            String ope_id_fk = retSampDef.getOpe_id_fk();  //p
            String ope_ver_fk = retSampDef.getOpe_ver_fk();  //p
            String tool_id_fk = retSampDef.getTool_id_fk();  //p
            String mdl_id_fk = retSampDef.getMdl_id_fk();  //p
            String path_id_fk = retSampDef.getPath_id_fk();  //p
            String path_ver_fk = retSampDef.getPath_ver_fk();  //p
            String ope_no_fk = retSampDef.getOpe_no_fk();  //p
            String samp_tpl_id_fk = retSampDef.getSamp_tpl_id_fk();  //p
            Integer samp_qty = retSampDef.getSamp_qty();
            Integer rsv_batch_qty = retSampDef.getRsv_batch_qty();
            String crt_note = retSampDef.getCrt_note();
            Integer samp_calc_typ = 1;  //p
            Integer samp_typ = null;   //p
            if (StringUtils.isBlank(tool_id_fk)) {
                samp_typ = 2;
            } else {
                samp_typ = 1;
            }
            retSampDef.setSamp_calc_typ(samp_calc_typ);
            retSampDef.setSamp_typ(samp_typ);
            retSampDef.setSamp_seq_id(samp_seq_id);
            retSampDef.setUnq_seq_id(unq_seq_id);
            //判断业务主键
            boolean exitPrimer = isExitPrimer(mdl_id_fk, ope_no_fk, ope_id_fk, ope_ver_fk, tool_id_fk, path_id_fk, path_ver_fk, samp_tpl_id_fk);
            if (exitPrimer == true) {
                primerIndex.add(i+1);
                continue;
            } else {
                retSmpDefDao.save(retSampDef);
            }
        }
        if (primerIndex != null && primerIndex.size() > 0) {
            fspsmpdefexport.setFlag(true);
            fspsmpdefexport.setIndex(primerIndex);
        }

        return fspsmpdefexport;


    }

    private boolean isExitPrimer(String mdl_id_fk,String ope_no_fk,String ope_id_fk,String ope_ver_fk,String tool_id_fk,String path_id_fk,
    String path_ver_fk,String samp_tpl_id_fk){
        String sql = "select * from ret_samp_def where 1 = 1";
        if (StringUtils.isNotBlank(mdl_id_fk)) {
            sql = sql + " and mdl_id_fk = '"+mdl_id_fk+"'";
        } else {
            sql = sql + " and mdl_id_fk is null";
        }
        if (StringUtils.isNotBlank(ope_no_fk)) {
            sql = sql + " and ope_no_fk = '"+ope_no_fk+"'";
        } else {
            sql = sql + " and ope_no_fk is null";
        }
        if (StringUtils.isNotBlank(ope_id_fk)) {
            sql = sql + " and ope_id_fk = '"+ope_id_fk+"'";
        } else {
            sql = sql + " and ope_id_fk is null";
        }
        if (StringUtils.isNotBlank(ope_ver_fk)) {
            sql = sql + " and ope_ver_fk = '"+ope_ver_fk+"'";
        } else {
            sql = sql + " and ope_ver_fk is null";
        }
        if (StringUtils.isNotBlank(tool_id_fk)) {
            sql = sql + "and tool_id_fk = '"+tool_id_fk+"'";
        } else {
            sql = sql + " and tool_id_fk is null";
        }
        if (StringUtils.isNotBlank(path_id_fk)) {
            sql = sql + " and path_id_fk = '"+path_id_fk+"'";
        } else {
            sql = sql + " and path_id_fk is null";
        }
        if (StringUtils.isNotBlank(path_ver_fk)) {
            sql = sql + " and path_ver_fk = '"+path_ver_fk+"'";
        } else {
            sql = sql + " and path_ver_fk is null";
        }

        if (StringUtils.isNotBlank(samp_tpl_id_fk)) {
            sql = sql + " and samp_tpl_id_fk = '"+samp_tpl_id_fk+"'";
        } else {
            sql = sql + " and samp_tpl_id_fk is null";
        }
        List<RetSampDef> list = retSmpDefDao.findBySQL(sql, RetSampDef.class);
        boolean flag = false;
        if (list != null && list.size()>0) {
            flag = true;
        }
        return flag;
    }


}
